<template>
	<view class="tab-bar">
		<scroll-view id="tab-bar" class="swiper-tab" scroll-x :scroll-left="scrollLeft">
			<view v-for="(tab,index) in tabBars" :key="tab.id" :class="['swiper-tab-list',tabIndex == index ? 'active' : '']" :id="tab.id"
			 :data-current="index" @click="tapTab(index)">{{tab.name}}</view>
		</scroll-view>
		<swiper :current="tabIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item class="ranking_list_1">
				<scroll-view class="goods_list" scroll-y>
					<textcom v-for="(item,i) in textcom[0].list" :key="i" v-bind:item="item" types="分享于"></textcom>
				</scroll-view>
			</swiper-item>
			<swiper-item class="ranking_list_2">
				<scroll-view class="goods_list" scroll-y>
					<textcom v-for="(item,i) in commision[0].list" :key="i" v-bind:item="item" types="返利时间" background="#ffa200"></textcom>
				</scroll-view>
			</swiper-item>
			<swiper-item class="ranking_list_3">
				<scroll-view class="goods_list" scroll-y>
					<textcom v-for="(item,i) in leveluser[0].list" :key="i" v-bind:item="item" images="true" background="#009f13"></textcom>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import textcom from '@/components/text-com';
	export default {
		components: {
			textcom
		},
		data() {
			return {
				loadingText: {
					contentdown: "上拉显示更多",
					contentrefresh: "正在加载...",
					contentnomore: "没有更多数据了"
				},
				scrollLeft: 0,
				isClickChange: false,
				tabIndex: 0,
				textcom:[{},{}],
				commision:[{},{}],
				leveluser:[{},{}],

				tabBars: [{
					name: '我的分享',
					id: 'system'
				}, {
					name: '我的返利',
					id: 'new'
				}, {
					name: '我的好友',
					id: 'transaction'
				}]
			}
		},
		onLoad(){
			this.loadData();
			setTimeout(()=> {
				this.renderImage = true;
			}, 300);
		},
		methods: {
			goDetail(e) {
				uni.navigateTo({
					url: '/pages/ucenter/mail_detail?id=' + e.id
				})
			},
			async changeTab(e) {
				let index = e.detail.current;
				if (this.isClickChange) {
					this.tabIndex = index;
					this.isClickChange = false;
					return;
				}
				let tabBar = await this.getElSize("tab-bar"),
					tabBarScrollLeft = tabBar.scrollLeft;
				let width = 0;

				for (let i = 0; i < index; i++) {
					let result = await this.getElSize(this.tabBars[i].id);
					width += result.width;
				}
				let winWidth = uni.getSystemInfoSync().windowWidth,
					nowElement = await this.getElSize(this.tabBars[index].id),
					nowWidth = nowElement.width;
				if (width + nowWidth - tabBarScrollLeft > winWidth) {
					this.scrollLeft = width + nowWidth - winWidth;
				}
				if (width < tabBarScrollLeft) {
					this.scrollLeft = width;
				}
				this.isClickChange = false;
				this.tabIndex = index; //一旦访问data就会出问题
			},
			getElSize(id) { //得到元素的size
				return new Promise((res, rej) => {
					uni.createSelectorQuery().select('#' + id).fields({
						size: true,
						scrollOffset: true
					}, (data) => {
						res(data);
					}).exec();
				});
			},
			async tapTab(index) { //点击tab-bar
				if (this.tabIndex === index) {
					return false;
				} else {
					let tabBar = await this.getElSize("tab-bar"),
						tabBarScrollLeft = tabBar.scrollLeft; //点击的时候记录并设置scrollLeft
					this.scrollLeft = tabBarScrollLeft;
					this.isClickChange = true;
					this.tabIndex = index;
				}
			},
			async loadData() {
				//评论数据
				let textcom = await this.$api.json('textcom');
				this.textcom = textcom;
				
				let commision = await this.$api.json('commision');
				this.commision = commision;
				
				let leveluser = await this.$api.json('leveluser');
				this.leveluser = leveluser;
				
			},
		}
	}
</script>

<style>
page { height:calc(100%)!important; }
.ranking_list_1,.ranking_list_2,.ranking_list_3 { width:100%; height:calc(100%)!important; overflow: auto; background:#ffffff; text-align:left; }
</style>
